import scrapy
from scrapy import Request
from scrapyExercise.items import LianjiaHomeItem

class LianjiaHomeSpider(scrapy.Spider):
    name = 'lianjia_home'

    def start_requests(self):
        url = "https://su.lianjia.com/ershoufang/"
        yield Request(url)

    def parse(self, response, **kwargs):
        try:
            list_selector = response.xpath("//li/div[@class='info clear']")
            for one_selector in list_selector:
                name = one_selector.xpath("div[@class='title']/a/text()").extract_first()
                other = one_selector.xpath("div[@class='address']/div[@class='houseInfo']/text()[last()]").extract_first()
                other_list = other.split('|')
                type = other_list[0].strip(" ")
                area = other_list[1].strip(" ")
                direction = other_list[2].strip(" ")
                fitment = other_list[3].strip(" ")
                elevator = other_list[4].strip(" ")
                total_price = one_selector.xpath("div[@class='priceInfo']/div[@class='totalPrice']/span/text()").extract_first()
                unit_price = one_selector.xpath("div[@class='priceInfo']/div[@class='unitPrice']/span/text()").extract_first()

                item = LianjiaHomeItem()
                item['name'] = name.strip(" ")
                item['type'] = type
                item['area'] = area
                item['direction'] = direction
                item['fitment'] = fitment
                item['elevator'] = elevator
                item['total_price'] = total_price
                item['unit_price'] = unit_price

                url = one_selector.xpath("div[@class='title']/a/@href").extract_first()
                yield Request(url, meta={'item':item}, callback=self.property_parse)
        except:
            pass

    def property_parse(self, response, **kwargs):
        item = response.meta["item"]
        item['property'] = '70'
        yield item